import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * 服务器集群管理
 * @date: 2019-7-9
 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { deleteServerCluster as _deleteServerCluster, fetchServerClusterList as _fetchServerClusterList, createServerCluster as _createServerCluster, updateServerCluster as _updateServerCluster, getServerClusterDetail as _getServerClusterDetail } from "../services/serverClusterService";
import { fetchServerList as _fetchServerList, fetchCanAssignList as _fetchCanAssignList, deleteServerAssign as _deleteServerAssign, createServerAssign as _createServerAssign } from "../services/serverService";
export default {
  namespace: 'serverCluster',
  state: {
    serverClusterList: [],
    // 服务器集群列表
    pagination: {},
    // 分页对象
    serverClusterDetail: {},
    // 服务器集群列表明细
    serverList: [],
    // 根据clusterid查询到的服务器列表
    canAssignList: [],
    // 可分配的服务器列表
    // serverAssignDetail: {}, // 服务器关联集群列表明细
    serverClusterPagination: {},
    serverPagination: {},
    canAssignPagination: {}
  },
  effects: {
    // 获取服务器集群列表
    fetchServerClusterList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchServerClusterList(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchServerClusterList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(_fetchServerClusterList, parseParameters(payload));

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverClusterList: list.content,
                  serverClusterPagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchServerClusterList);
    }),
    // 查询服务器集群列表详情
    getServerClusterDetail: /*#__PURE__*/_regeneratorRuntime.mark(function getServerClusterDetail(_ref3, _ref4) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function getServerClusterDetail$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(_getServerClusterDetail, payload);

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverClusterDetail: list
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, getServerClusterDetail);
    }),
    // 新增服务器集群
    createServerCluster: /*#__PURE__*/_regeneratorRuntime.mark(function createServerCluster(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createServerCluster$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_createServerCluster, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, createServerCluster);
    }),
    // 更新服务器集群
    updateServerCluster: /*#__PURE__*/_regeneratorRuntime.mark(function updateServerCluster(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateServerCluster$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_updateServerCluster, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateServerCluster);
    }),
    // 删除服务器集群
    deleteServerCluster: /*#__PURE__*/_regeneratorRuntime.mark(function deleteServerCluster(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteServerCluster$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_deleteServerCluster, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteServerCluster);
    }),
    // 根据clusterid查询服务器列表
    fetchServerList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchServerList(_ref11, _ref12) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchServerList$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call, put = _ref12.put;
              _context6.next = 4;
              return call(_fetchServerList, parseParameters(payload));

            case 4:
              res = _context6.sent;
              list = getResponse(res);

              if (!list) {
                _context6.next = 9;
                break;
              }

              _context6.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverList: list.content,
                  serverPagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchServerList);
    }),
    // 查询可分配的服务器列表
    fetchCanAssignList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCanAssignList(_ref13, _ref14) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchCanAssignList$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call, put = _ref14.put;
              _context7.next = 4;
              return call(_fetchCanAssignList, parseParameters(payload));

            case 4:
              res = _context7.sent;
              list = getResponse(res);

              if (!list) {
                _context7.next = 9;
                break;
              }

              _context7.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  canAssignList: list.content,
                  canAssignPagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context7.stop();
          }
        }
      }, fetchCanAssignList);
    }),
    // 删除服务器的集群分配
    deleteServerAssign: /*#__PURE__*/_regeneratorRuntime.mark(function deleteServerAssign(_ref15, _ref16) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteServerAssign$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call;
              _context8.next = 4;
              return call(_deleteServerAssign, payload);

            case 4:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, deleteServerAssign);
    }),
    // 创建服务器的集群分配
    createServerAssign: /*#__PURE__*/_regeneratorRuntime.mark(function createServerAssign(_ref17, _ref18) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createServerAssign$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              _context9.next = 4;
              return call(_createServerAssign, payload);

            case 4:
              res = _context9.sent;
              return _context9.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, createServerAssign);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref19) {
      var payload = _ref19.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};